<?php
/*
 * scrumbrella - Scrum board.
 *
 * Copyright (C) 2009  Gustavo N. Borgoni <gborgoni@voicemedia.com.ar>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

require_once("db_schema.php");

/*! \brief return the unit list
 *  \retval -1 error
 *  \retval Array with unit list
 */
function unit_list()
{
	global $unit_table;

	$SQL = "SELECT * FROM $unit_table";
	$res = db_query($SQL);

	if (db_error($SQL)) {
		logger_fatal("Error while list units " . db_error_message($res));
		return -1;
	}

	if (db_numrows($res) > 0) {
		$unit_count = 0;
		while ($row = db_fetchrow($res)) {
			foreach ($row AS $key => $value) {
				$units[$unit_count][$key] = $value;
			}
			$unit_count++;
		}
		db_freeresult($res);

		return $units;
	} else {
		return 0;
	}
}

/*! \brief return the unit name of an id
 *  \param $id int id of unit time
 *  \retval < 0 Error
 *  \retval string unit time name
 */
function unit_name($id) 
{
	global $unit_table;

	$SQL = "SELECT * FROM $unit_table WHERE id_unit=$id";
	$res = db_query($SQL);

	if (db_error($SQL)) {
		logger_error(" Error when load unit name " . db_error_message($res));
		return -1;
	}

	if (db_numrows($res) > 0) {
		while ($row = db_fetchrow($res)) {
			$name = $row['name'];
		}
		db_freeresult($res);

		return $name;
	} else {
		return 0;
	}
}
?>
